import { Component, OnInit ,Input} from '@angular/core';
import {book} from '../book';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { pub2sub } from '../pub2subService';


@Component({
  selector: 'app-book-list',
  templateUrl: './book-list.component.html',
  styleUrls: ['./book-list.component.css']
})

export class BookListComponent implements OnInit {
  books:book[]=[];
  constructor(
    private route: ActivatedRoute,
    private location: Location,
    private pub2sub:pub2sub
  ) {}
  ngOnInit(): void {
    this.pub2sub.subscribe('book-list','showBooks',(books:book[])=>{
      this.books = books;
    })
    // this.route.url.subscribe(args=>{
    //   const id = this.route.snapshot.paramMap.get('id') || '';


    //   fetch('/app/getBooks').finally(()=>{
    //     this.books=[{
    //       id:1,
    //       name:id,
    //       author:'null'
    //     },{
    //       id:2,
    //       name:id+id,
    //       author:'null'
    //     }]
    //   })

    // })

  }
addBook(bookName:HTMLInputElement,authorName:HTMLInputElement){
  if(!bookName.value) return;
  let book = {
    name:bookName.value,
    author:authorName.value
  }
  bookName.value='';
  authorName.value='';
  this.pub2sub.notify('shell','save-new-book',book,false)

}
delBook(id:number){  
  this.pub2sub.notify('shell','del-book',id,false);
}
}

